package com.xuan.pmh.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.xuan.pmh.R;
import com.xuan.pmh.bean.Worker;
import com.xuan.pmh.util.FinalValue;
import com.xuan.pmh.util.ToastUtil;

/**
 * @author xuan 2014-10-30
 * 
 */
public class ManagerWorkActivity extends HttpActivity {

	private LinearLayout llManageWork;
	private Map<String,View> workerViews = new HashMap<String,View>();
	final int GET_NOT_SURE_WORKER = 0;
	final int SURE_WORKER = 1;
	final int REFUCE_WORKER = 2;
	
	
	@Override
	public void afterPost(String result, int requestCode,Map<String, String> request) {
		// TODO Auto-generated method stub
		gsonBean = getGsonBean(result);
		switch (requestCode) {
		case GET_NOT_SURE_WORKER:
			if (gsonBean.state == FinalValue.SUCCESS) {
				List<Worker> workers = gson.fromJson(gsonBean.bean,
						new TypeToken<List<Worker>>() {
						}.getType());
				for (int i = 0; i < workers.size(); i++) {
					View view = getWorkerView(workers.get(i));
					llManageWork.addView(view);
					workerViews.put(workers.get(i).userId, view);
				}
			}
			break;
		case SURE_WORKER:
		case REFUCE_WORKER:
			if (gsonBean.state == FinalValue.SUCCESS) {
				ToastUtil.show(this, "操作成功");
				llManageWork.removeView(workerViews.get(request.get("param")));
			}
			break;
		}
	}

	public View getWorkerView(Worker worker) {
		View view = inflater.inflate(R.layout.item_user_manage, null);
		TextView tvUserId, tvName, tvPhone, tvBirthday, tvSex;
		Button btSure,btRefuse;
		tvUserId = (TextView) view.findViewById(R.id.tvUserId);
		tvName = (TextView) view.findViewById(R.id.tvName);
		tvPhone = (TextView) view.findViewById(R.id.tvPhone);
		tvBirthday = (TextView) view.findViewById(R.id.tvBirthday);
		tvSex = (TextView) view.findViewById(R.id.tvSex);
		btSure = (Button) view.findViewById(R.id.btSure);
		btRefuse = (Button) view.findViewById(R.id.btRefuse);
		
		btSure.setTag(worker.userId);
		btSure.setOnClickListener(onClick);
		btRefuse.setTag(worker.userId);
		btRefuse.setOnClickListener(onClick);
		tvUserId.setText(worker.userId);
		tvName.setText(worker.name);
		tvPhone.setText(worker.phone);
		tvBirthday.setText(worker.birthday);
		if (worker.sex.equals("m")) {
			tvSex.setText("男");
		} else {
			tvSex.setText("女");
		}
		Log.v("worker", gson.toJson(worker));
		return view;

	}

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_manage_work;
	}

	@Override
	public void setUI() {
		// TODO Auto-generated method stub
		llManageWork = (LinearLayout) findViewById(R.id.llManageWork);
		paramsList.clear();
		addPostRequest("getNotSureWorker", paramsList, GET_NOT_SURE_WORKER);
		sendPostRequest();
	}

	@Override
	public void OnClickListener(View view) {
		// TODO Auto-generated method stub
		switch(view.getId()){
		case R.id.btSure:
			paramsList.clear();
			paramsList.put("param", view.getTag().toString());
			addPostRequest("sureUser", paramsList , SURE_WORKER);
			sendPostRequest();
			break;
		case R.id.btRefuse:
			paramsList.clear();
			paramsList.put("param", view.getTag().toString());
			addPostRequest("refuseUser", paramsList , REFUCE_WORKER);
			sendPostRequest();
			break;
		}
	}

}
